import * as mobx from 'mobx';
import { observable, action, computed, asMap } from 'mobx';
import { freeFlowRead,freeFlowSave } from '../apis/req';

export class FreeFlowStore{

    baseParams = {};
    @observable loading = false;
    @observable columns = [];
    @observable datas = [];
    stepindex = 0;
    saveEvent = null;
    closeEvent = null;

    constructor(){
        this.setBaseParams = this.setBaseParams;
        this.doInit = this.doInit;
        this.addStepIndex = this.addStepIndex;
        this.setDatas = this.setDatas;
        this.setSaveEvent = this.setSaveEvent;
        this.setCloseEvent = this.setCloseEvent;
        this.resetStore = this.resetStore;
    }

    @action
    setBaseParams = (params={}) => {
        this.baseParams = params;
    }

    doInit = () => {
        this.loading = true;
        freeFlowRead(this.baseParams).then(data =>{
            this.loading = false;
            const { columns,datas } = data;
            this.columns = columns;
            this.datas = datas;
            this.stepindex = this.datas.length;
        });
    }

    addStepIndex = () => {
        this.stepindex++;
    }

    setDatas = (datas=[]) => {
        this.datas = datas;
    }

    doSave = (directSubmit=false) => {
        let params = {...this.baseParams};
        const rownum = this.datas.length;
        rownum > 0 && this.datas.map((row,index) =>{
            params[`floworder_${index}`] = index;
            params[`nodename_${index}`] = row.stepname;
            params[`Signtype_${index}`] = row.signtype;
            params[`operators_${index}`] = row.operators;
        });
        params["rownum"] = rownum;
        params["indexnum"] = rownum;
        this.loading = true;
        freeFlowSave(params).then(data =>{
            this.loading = false;
            this.saveEvent(directSubmit);
        });
    }

    setSaveEvent = (fun) => {
        this.saveEvent = fun;
    }
    
    setCloseEvent = (fun) => {
        this.closeEvent = fun;
    }

    resetStore = () => {
        this.baseParams = {};
        this.stepindex = 0;
        this.datas = [];
    }

}